﻿// <copyright file="ColorChooser.xaml.cs" company="Oleg V. Polikarpotchkin">
// Copyright © 2009 Oleg V. Polikarpotchkin. All Right Reserved
// </copyright>
// <author>Oleg V. Polikarpotchkin</author>
// <email>ov-p@yandex.ru</email>
// <date>2009-06-12</date>
// <summary>OpenWPFChart library.
// ColorChooser User Control.
// </summary>
// <revision>$Id$</revision>

using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace OpenWPFChart.Helpers.ChooseColorDialog
{
	/// <summary>
	/// ColorChooser User Control.
	/// </summary>
	public partial class ColorChooser : UserControl
	{
		ColorChooserViewModel viewModel;
		/// <summary>
		/// Initializes a new instance of the <see cref="ColorChooser"/> class.
		/// </summary>
		public ColorChooser()
		{
			InitializeComponent();

			viewModel = new ColorChooserViewModel();
			viewModel.PropertyChanged += viewModel_PropertyChanged;
			contentHost.DataContext = viewModel;
		}

		/// <summary>
		/// Handles the PropertyChanged event of the viewModel object.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="System.ComponentModel.PropertyChangedEventArgs"/> 
		/// instance containing the event data.</param>
		void viewModel_PropertyChanged(object sender, PropertyChangedEventArgs e)
		{
			if (e.PropertyName == "SelectedColor")
				SelectedColor = viewModel.SelectedColor;
			else if (e.PropertyName == "IsColorPickerVisible")
				IsColorPickerVisible = viewModel.IsColorPickerVisible;
		}

		#region SelectedColor
		/// <summary>
		/// SelectedColor backing DependencyProperty.
		/// </summary>
		private static readonly DependencyProperty SelectedColorProperty
			= DependencyProperty.Register("SelectedColor", typeof(Color), typeof(ColorChooser),
				new FrameworkPropertyMetadata(Colors.Transparent
					, FrameworkPropertyMetadataOptions.AffectsRender
						| FrameworkPropertyMetadataOptions.BindsTwoWayByDefault
					, SelectedColorPropertyChanged));
		/// <summary>
		/// Gets or sets the selected color.
		/// </summary>
		/// <value>The color selected.</value>
		public Color SelectedColor
		{
			get { return (Color)GetValue(SelectedColorProperty); }
			set { SetValue(SelectedColorProperty, value); }
		}
		/// <summary>
		/// SelectedColorProperty changed event handler.
		/// </summary>
		/// <param name="d">The sender.</param>
		/// <param name="e">The <see cref="System.Windows.DependencyPropertyChangedEventArgs"/> 
		/// instance containing the event data.</param>
		static void SelectedColorPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
		{
			ColorChooser ths = d as ColorChooser;
			if (ths == null)
				return;

			Color color = (Color)(e.NewValue);
			ths.viewModel.Opacity = color.A / 255.0;
			ths.viewModel.OpaqueColor = color;

			ths.RaiseEvent(new SelectedColorChangedEventArgs(color, SelectedColorChangedEvent, ths));
		}
		#endregion SelectedColor

		#region SelectedColorChangedEvent
		/// <summary>
		/// SelectedColorChangedEvent backing RoutedEvent.
		/// </summary>
		public static readonly RoutedEvent SelectedColorChangedEvent
			= EventManager.RegisterRoutedEvent("SelectedColorChanged", RoutingStrategy.Bubble
				, typeof(SelectedColorChangedEventHandler), typeof(ColorChooser));
		/// <summary>
		/// SelectedColorChangedEvent accessors.
		/// </summary>
		public event SelectedColorChangedEventHandler SelectedColorChanged
		{
			add { AddHandler(SelectedColorChangedEvent, value); }
			remove { RemoveHandler(SelectedColorChangedEvent, value); }
		}
		#endregion SelectedColorChangedEvent

		#region IsColorPickerVisible
		/// <summary>
		/// IsColorPickerVisible backing DependencyProperty.
		/// </summary>
		private static readonly DependencyProperty IsColorPickerVisibleProperty
			= DependencyProperty.Register("IsColorPickerVisible", typeof(bool), typeof(ColorChooser),
				new FrameworkPropertyMetadata(false
					, FrameworkPropertyMetadataOptions.AffectsMeasure
						| FrameworkPropertyMetadataOptions.BindsTwoWayByDefault
					, IsColorPickerVisiblePropertyChanged));
		/// <summary>
		/// Gets or sets a value indicating whether the color picker is visible.
		/// </summary>
		/// <value>
		/// 	<c>true</c> if the color picker is visible; otherwise, <c>false</c>.
		/// </value>
		public bool IsColorPickerVisible
		{
			get { return (bool)GetValue(IsColorPickerVisibleProperty); }
			set { SetValue(IsColorPickerVisibleProperty, value); }
		}
		/// <summary>
		/// IsColorPickerVisibleProperty changed event handler.
		/// </summary>
		/// <param name="d">The sender.</param>
		/// <param name="e">The <see cref="System.Windows.DependencyPropertyChangedEventArgs"/> 
		/// instance containing the event data.</param>
		static void IsColorPickerVisiblePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
		{
			ColorChooser ths = d as ColorChooser;
			if (ths == null)
				return;
			ths.viewModel.IsColorPickerVisible = (bool)e.NewValue;
		}
		#endregion SelectedColor
	}
}
